// pages/citySelect/citySelect.js
import ajax from "../../utils/index"
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    popularCitie: [],
    region: [],
    regionAll: [],
    locatingInfo: false,
    city: '',
    search: '',
    locationEvent: null
  },
  searchConfirm() {
    const { regionAll, search } = this.data
    if (!search) return
    const reg = /^[A-Z]{1}$/
    if (reg.test(search.toUpperCase())) {
      console.log(`label-${search.toUpperCase()}`)
      return wx.pageScrollTo({ selector: `#label-${search.toUpperCase()}` })
    }
    const entity = regionAll.find(item => item.regionName.indexOf(search) !== -1)
    if (!entity) return
    wx.pageScrollTo({ selector: `#a-${entity.id}` })
  },
  searchChange(event) {
    this.setData({ search: event.detail.value })
  },
  selectEvent(event) {
    const item = event.currentTarget.dataset.item
    const eventChannel = this.getOpenerEventChannel()
    eventChannel.emit('backEvent', item);
    wx.navigateBack()
  },
  async getRequestList() {
    const res = await ajax.get('/region/sortCityList').finally(() => wx.stopPullDownRefresh());
    let region = res.data.region || []
    let labelArr = new Set()
    region.forEach(item => labelArr.add(item.letter))
    let entityArr = []
    labelArr.forEach(item => {
      let arr = region.filter(fil => fil.letter == item)
      entityArr.push({ key: item, list: arr })
    })
    const popularList = res.data.popularCitie.map(item => {
      item.regionName = item.name
      return item
    })
    const popularCitie = ajax.listFormat(popularList, 4)
    for (let index = 0; index < popularCitie.length; index++) {
      const item = popularCitie[index], count = 4 - item.length
      if (item.length < 4) for (let i = 0; i < count; i++) item.push(null)
    }
    this.setData({ popularCitie: popularCitie, region: entityArr, regionAll: region })
  },
   /**
   * 获取定位授权
   */
  async getLocation() {
    const res = await ajax.getLocationQQMap()
    console.log('getLocation', res)
    this.setData({ locationEvent: res })
  },
  /**
   * 开启设置页面
   */
  async openSetting() {
    const setting = await ajax.openSetting()
    if (setting['scope.userLocation']) this.getSetting()
  },
  /**
   * 查看授权
   */
  async getSetting() {
    const setting = await ajax.getSetting()
    this.setData({locatingInfo: !setting['scope.userLocation']})
    this.getLocation()
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({ city: options.city })
    this.getSetting()
    this.getRequestList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getRequestList()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})